package com.lk.springboot.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.lk.springboot.dao.HomeMapper;
import com.lk.springboot.service.HomeSevice;
import com.lk.springboot.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lk
 * @date 2020/3/5 0005 12:12
 */
@Service
public class HomeServiceImpl implements HomeSevice {

    @Autowired
    HomeMapper homeMapper;

    @Override
    public List<MyProps> findAllMyProps(String u_id) {
        return homeMapper.getMyProps(u_id);
    }

    @Override
    public List<JSONObject> findAllMyAchievements(String u_id) {
        List<MyUmbrella> myUmbrellas = homeMapper.getMyUmbrella(u_id);
        String u_name = homeMapper.getUserName(u_id);
        ArrayList<JSONObject> list = new ArrayList<>();
        for (MyUmbrella myUmbrella : myUmbrellas) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("userName", u_name);
            jsonObject.put("date", myUmbrella.getDate());
            jsonObject.put("umId", myUmbrella.getUm_id());
            jsonObject.put("place", myUmbrella.getPlace());
            list.add(jsonObject);
        }
        return list;
    }

    @Override
    public List<JSONObject> findAllMyPictures(String u_id) {
        List<MySmallPhoto> mySmallPhotoList = homeMapper.getAllMySP(u_id);
        ArrayList<JSONObject> list = new ArrayList<>();
        for (MySmallPhoto mySmallPhoto : mySmallPhotoList) {
            JSONObject jsonObject = new JSONObject();
            String n = homeMapper.getSitesName(mySmallPhoto.getS_id());
            TheSiteSP theSiteSP = homeMapper.getTheSiteSP(mySmallPhoto.getSp_id());
            jsonObject.put("name", n + "0" + theSiteSP.getTags());
            jsonObject.put("url", theSiteSP.getUrl());
            jsonObject.put("num", mySmallPhoto.getNum());
            list.add(jsonObject);
        }
        return list;
    }

    @Override
    public String getMine(String u_id) {
        Result result = new Result(ResultCode.SUCCESS);
        result.setData(homeMapper.getMine(u_id));
        return JSONObject.toJSONString(result);
    }

    @Override
    public String addLikes(String u_id) {
        Result result = new Result(ResultCode.SUCCESS);
        try {
            homeMapper.addLikes(u_id);
            result.setData(homeMapper.getLikes(u_id));
        } catch (Exception e) {
            result.setData("filed");
        }
        return JSONObject.toJSONString(result);
    }

    @Override
    public String reducePropsNum(String u_id, int props_id) {
        Result result = null;
        int propsNum = homeMapper.getPropsNum(u_id, props_id);
        if (propsNum > 0) { // 当道具数量大于0时进行
            result = new Result(ResultCode.SUCCESS);
            try {
                homeMapper.reducePropsNum(u_id, props_id); // 对应道具减少一个
                result.setData(propsNum - 1); // 返回用户剩余道具数目
            } catch (Exception e) {
                result.setData("failed");
            }
        } else {
            result = new Result(ResultCode.DONNOT_ENOUGH);
            result.setData("您的道具不够了");
        }
        return JSONObject.toJSONString(result);
    }

    @Override
    public void setNewsStatus(String u_id) {
        homeMapper.updateNewsNo(u_id);// 0无，1有
    }

    @Override
    public String getNewsStatus(String u_id) {
        boolean newsStatus = false;// 默认无新消息
        if (homeMapper.getNews(u_id) == 1) {// 1,有新消息
            newsStatus = true;
        }
        Result result = new Result(ResultCode.SUCCESS);
        result.setData(newsStatus);
        return JSONObject.toJSONString(result);
    }

}
